import fs from "fs";
import path from "path";
import type { Configuration as DevServerConfiguration } from "webpack-dev-server";
import { ROOT_DIR } from "./webpack.const";
import { Configuration } from "webpack";

const serverConfig: DevServerConfiguration = {
  port: 44388,
  // 不打开浏览器，会影响编译速度
  open: false,
  server: {
    type: "https",
    options: {
      minVersion: "TLSv1.3",
      key: fs.readFileSync(path.join(ROOT_DIR, "private.key")),
      cert: fs.readFileSync(path.join(ROOT_DIR, "certificate.crt")),
      passphrase: "webpack-dev-server",
    },
  },
  watchFiles: {
    paths: [path.join(ROOT_DIR, "./src")],
    options: {
      usePolling: true,
      interval: 1000,
      ignored: [
        path.join(ROOT_DIR, "./dist"),
        path.join(ROOT_DIR, "./node_modules"),
      ],
    },
  },
  // 使用 gzip 压缩传输 http 数据
  compress: true,
  // 暂时没有静态文件选项，注释掉该配置。
  // 这个配置会从根目录下的 public 文件夹创建一个文件服务器，然后通过 /static 路径访问。
  // static: {
  //   directory: path.join(ROOT_DIR, "./public"),
  //   publicPath: "/static",
  // },
  client: {
    overlay: {
      errors: true,
      warnings: false,
    },
    progress: true,
  },
};

const config: Configuration = {
  devServer: serverConfig,
};

export default config;
